#include "config.h"
#include <QSettings>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>

static QString userConfigPath() {
    QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
    QDir dir;
    if(!dir.exists(path)) {
        if(!dir.mkpath(path))
            qDebug() << "mkpath : " << path << " fail. ";
    }
    return path;
}

static QString userConfigFilePath() {
    return userConfigPath() + "/" + "config.ini";
}

Config::Config() {
    loadConfig();
}

Config::~Config()
{
    saveConfig();
}

Config &Config::instance()
{
    static Config cfg;
    return cfg;
}

void Config::loadConfig()
{
    QSettings setting(userConfigFilePath(), QSettings::IniFormat);
    setting.beginGroup("APP");
    intervalMinutes = setting.value("intervalMinutes", 30).toInt();
    flickInterval = setting.value("flickInterval", 500).toInt();
    startTip = setting.value("startTip", "启动了").toString();
    timedTip = setting.value("timedTip", "时间到").toString();
    changeTips = setting.value("changeTips", "更改").toString();
    setting.endGroup();
}

void Config::saveConfig()
{
    QSettings setting(userConfigFilePath(), QSettings::IniFormat);
    setting.beginGroup("APP");
    setting.setValue("intervalMinutes", intervalMinutes);
    setting.setValue("flickInterval", flickInterval);
    setting.setValue("startTip", startTip);
    setting.setValue("timedTip", timedTip);
    setting.setValue("changeTips", changeTips);
    setting.endGroup();
}
